package jnpf.database.source.impl;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import jnpf.base.DbTableModel;
import jnpf.database.enums.DataTypeEnum;
import jnpf.database.enums.DbAliasEnum;
import jnpf.database.enums.DbSttEnum;
import jnpf.database.model.DbTableFieldModel;
import jnpf.database.model.dto.DbTableDTO;
import jnpf.database.source.DbBase;
import jnpf.database.util.DbUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DbPostgre extends DbBase {


    @Override
    public void setDbType() {
        dbEncode = "PostgreSql";
        driver = "org.postgresql.Driver";
        fieldSql = "";
        listSql =
                "SELECT t.TABLE_NAME AS "+ DbAliasEnum.TABLE_NAME.AS()+",c.COMMENTS AS "+DbAliasEnum.TABLE_COMMENT.AS()+" FROM\n" +
                        "information_schema.TABLES AS t\n" +
                        "LEFT JOIN\n" +
                        "(SELECT TABLE_NAME,COMMENTS FROM DBA_TAB_COMMENTS)AS c\n" +
                        "ON\n" +
                        "t.TABLE_NAME = c.TABLE_NAME\n" +
                        "\n" +
                        "WHERE\n" +
                        " TABLE_SCHEMA = '"+ DbSttEnum.DB_SCHEMA.getTarget()+"'";

    }

    @Override
    public String getDataType(DataTypeEnum dte){
        return dte.getPostgreFieldType();
    }
    
    @Override
    public String getConnectionUrl(String host, String port, String dbName) {
        return "jdbc:postgresql://"+ host +":"+ port +"/"+ dbName;
    }

    @Override
    public void setDynamicMap(HashMap<String, TableNameHandler> map, DbTableModel dbTableModel) {

    }

    @Override
    public DbTableModel getTableModel(ResultSet result) throws SQLException {
        return DbUtil.getTableModelCommon(result,
                true,
                true,
                true,
                false,
                false,
                false
        );
    }

    @Override
    public DbTableFieldModel getFieldModel(ResultSet result, Map<String, Object> map) throws SQLException {
        return null;
    }

    @Override
    public void createTable(DbTableDTO dto) {

    }

    @Override
    public void updateTable(DbTableDTO dto) {

    }

    @Override
    public String getDbTime(Connection conn) {
        return null;
    }

    @Override
    public String getSqlByStructure(String sql, Connection conn, String table, String tableSpace) {
        return sql.
                replace(DbSttEnum.TABLE.getTarget(),table).
                replace(DbSttEnum.DB_NAME.getTarget(), DbSttEnum.DB_NAME.getValByConn(conn)).
                replace(DbSttEnum.DB_SCHEMA.getTarget(),DbSttEnum.USER_NAME.getValByConn(conn)).
                replace(DbSttEnum.TABLE_SPACE.getTarget(),tableSpace);
    }
}
